home *** CD-ROM | disk | FTP | other *** search
- /*++
- /* NAME
- /* chrootuid 1
- /* SUMMARY
- /* run command in restricted environment
- /* SYNOPSIS
- /* chrootuid newroot newuser command...
- /* DESCRIPTION
- /* The \fIchrootuid\fR command sets up a restricted environment for
- /* command execution. Access to the file system is restricted to
- /* the \fInewroot\fR subtree; privileges are restricted to those of
- /* the \fInewuser\fR account (which must be a known account in the
- /* unrestricted environment).
- /* The initial working directory is changed to \fInewroot\fR.
- /*
- /* \fIchrootuid\fR combines chroot(8) and su(1) into one program, so
- /* that there is no need to have commands such as /usr/bin/su
- /* in the restricted environment.
- /*
- /* Only the superuser can use the \fIchrootuid\fR command.
- /* SEE ALSO
- /* chroot(8), su(1)
- /* DIAGNOSTICS
- /* Problems are reported to the syslog daemon.
- /* AUTHOR(S)
- /* W.Z. Venema
- /* Eindhoven University of Technology
- /* Department of Mathematics and Computer Science
- /* Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
- /* CREATION DATE
- /* Tue Oct 13 11:37:29 MET 1992
- /* LAST MODIFICATION
- /* 93/08/15 22:19:27
- /* VERSION/RELEASE
- /* 1.2
- /*--*/
-
- #ifndef lint
- static char sccsid[] = "@(#) chrootuid.c 1.2 93/08/15 22:19:27";
- #endif
-
- /* System libraries. */
-
- #include <pwd.h>
- #include <syslog.h>
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- struct passwd *pwd;
-
- /*
- * Open a channel to the syslog daemon. Older versions of openlog()
- * require only two arguments.
- */
-
- #ifdef LOG_DAEMON
- (void) openlog(argv[0], LOG_PID, LOG_DAEMON);
- #else
- (void) openlog(argv[0], LOG_PID);
- #endif
-
- /*
- * Require proper amount of arguments. In all cases of error, exit with
- * zero status because we have already reported the problem via syslogd.
- * No need to make inetd complain, too.
- */
-
- if (argc < 4) {
- syslog(LOG_ERR, "usage: %s path user command", argv[0]);
- return (0);
- }
- /* Must step into the new subtree. */
-
- if (chdir(argv[1])) {
- syslog(LOG_ERR, "chdir(%s): %m", argv[1]);
- return (0);
- }
- /* The user must be known in the *unrestricted* universe... */
-
- if ((pwd = getpwnam(argv[2])) == 0) {
- syslog(LOG_ERR, "%s: user unknown", argv[2]);
- return (0);
- }
- /* Do the chroot() before giving away root privileges. */
-
- if (chroot(argv[1])) {
- syslog(LOG_ERR, "chroot(%s): %m", argv[1]);
- return (0);
- }
- /* Switch group id then user id. */
-
- if (setgid(pwd->pw_gid)) {
- syslog(LOG_ERR, "setgid(%d): %m", pwd->pw_gid);
- return (0);
- }
- if (setuid(pwd->pw_uid)) {
- syslog(LOG_ERR, "setuid(%d): %m", pwd->pw_uid);
- return (0);
- }
- /* In case we still have the /etc/passwd file still open. */
-
- endpwent();
-
- /* Run the command and hope for the best. */
-
- (void) execvp(argv[3], argv + 3);
- syslog(LOG_ERR, "%s: %m", argv[3]);
- return (0);
- }
-